/* 
 * File:   staticutils.cpp
 * Author: pable
 * 
 * Created on November 26, 2008, 10:15 AM
 */

#define PRINT_PAGE 1000

#include "staticutils.h"



StaticUtils::StaticUtils() {

}

bool StaticUtils::findQuote(std::string & path, VectorString& vectorPath) {
    int index = 0;
    int find = 0;
    if (path[0] == '\"') {
        path.erase(0, 1);
        find = path.find_first_of("\"", index);
        if (find == -1) {
            throw 0;
        }
        vectorPath.push_back(path.substr(index, find));
        path = path.substr(find + 1, path.length() - (find + 1));
        path = StaticUtils::trim(path);
    }
}

std::string StaticUtils::extractFileName(std::string path) {
    std::string aux;
    int index = 0;
    int find = 0;
    VectorString vector = StaticUtils::split(path,"/");
    std::string value = vector.at(vector.size() - 1);
    aux=value;
    while ((unsigned int) find < value.length() && find != -1) {
        if (value.length() > 0) {
            find = aux.find_last_of("\\.",index);
            if (find == -1) {
                break;
            }
            aux = aux.substr(find+1);
        }
    }
    return aux;
}

VectorString StaticUtils::split(std::string string, std::string separator) {
    VectorString path;
    std::string element;
    int index = 0;
    int find = 0;
    while ((unsigned int) find < string.length() && find != -1) {
        //findQuote(string, path);
        if (string.length() > 0) {
            find = string.find_first_of(separator, index);
            if (find == -1) {
                element = string.substr(0, string.length());
                path.push_back(element);
                break;
            }
            element = string.substr(0, find);
            element = StaticUtils::trim(element);

            string = string.substr(find + 1, string.length() - (find + 1));
            if (string != " ")
                string = StaticUtils::trim(string);
            path.push_back(element);
            find = 0;
        }
    }
    return path;
}

void StaticUtils::printStringFormat(std::string& buffToPrint) {
    bool bufferEmpty = false;
    std::string printString = "";
    while (!bufferEmpty) {
        if (buffToPrint.length() > PRINT_PAGE) {
            int size = buffToPrint.size();
            printString = buffToPrint.substr(0, PRINT_PAGE);
            buffToPrint = buffToPrint.substr(PRINT_PAGE + 1, size - 1);
            continue;
        }
        bufferEmpty = true;
        std::cout << buffToPrint;
        fflush(stdout);
    }
}

bool StaticUtils::isNumeric(std::string aString) {
  std::istringstream inpStream(aString);
  int inpValue=0;
  if (inpStream>>inpValue)
  {
      return true;
  }
  else
  {
      return false;
  }
}

std::string& StaticUtils::trimo(std::string& string) {
    while (string[0] == ' ')
        string = string.substr(1, string.length() - 1);
    while (string[string.length() - 1] == ' ')
        string = string.substr(0, string.length() - 1);
    return string;
}

std::string StaticUtils::trim(std::string string) {
    while (string[0] == ' ')
        string = string.substr(1, string.length() - 1);
    while (string[string.length() - 1] == ' ')
        string = string.substr(0, string.length() - 1);
    return string;
}



std::string::iterator StaticUtils::getAtBuffer(std::string& inputBuffer, int pos) {
    std::string::iterator iteBuffer = inputBuffer.begin();
    for (int i = 0; i < pos; i++) {
        if (iteBuffer == inputBuffer.end()) {
            throw std::exception();
        }
        iteBuffer++;
    }
    return (iteBuffer);
}




StaticUtils::~StaticUtils() {

}




